using UnityEngine;
using System.Collections;

public class MainMenuGUI : MonoBehaviour {

	public string levelLoad;
	public Texture gameTitle;
	public GameObject storage;
	public GameObject soundClick;
	public GameObject storageObject;
	
	public float Sh;
	public float Sw;
	public bool once;
	public int newGameText;
	public string profile;
	
	public string[] Profiles;
	// Use this for initialization
	void Start () {
		
		newGameText = 0;
		once = false;
		for(int q = 0; q<Profiles.Length; q++)
			{
				Profiles[q] = PlayerPrefs.GetString("pro"+q);
			}
	}
	
	// Update is called once per frame
	void OnGUI () {
		Sh = Screen.height;
		Sw = Screen.width;
		
		if(newGameText==0)
		{
			GUI.DrawTexture(new Rect((Sw/2)-(gameTitle.width/2), Sh*.1f, Sw*.6f,Sh*.3f), gameTitle, ScaleMode.ScaleToFit, true, 0);
			if (GUI.Button(new Rect(Sw*.5f-50, Sh*.5f, 100, 40), "New Game"))
			{
				Instantiate(soundClick,transform.position,transform.rotation);
				newGameText = 1;
			}
			if (GUI.Button(new Rect(Sw*.5f-50, Sh*.6f, 100, 40), "Load Profile"))
			{
				Instantiate(soundClick,transform.position,transform.rotation);
				newGameText = 2;
			}
		}
		if(newGameText==1)
		{
			profile = GUI.TextField (new Rect (Sw*.5f-65, Sh*.5f-15, 130, 30), profile, 18);
			if(Input.GetKey(KeyCode.Return)&&profile!="")
			{
				if(!once)
				{
					for(int q = 0; q<Profiles.Length; q++)
					{
						if(Profiles[q]=="")
						{
							PlayerPrefs.SetString("pro"+q, profile);
							break;
						}
					}
					Instantiate(storage,transform.position,transform.rotation);
					storageObject = GameObject.FindWithTag("storage");
					storageObject.GetComponent<storage>().currentProfile = profile;
					Instantiate(soundClick,transform.position,transform.rotation);
					Application.LoadLevel(levelLoad);
					once = true;
				}
			}
		}
		if(newGameText ==2)
		{
			GUILayout.BeginArea(new Rect(0,0,Sw,Sh));
			GUILayout.BeginVertical();
			GUILayout.Space(20);
	
			//First Horizontal
			GUILayout.BeginHorizontal();
		
			for (int q = 0; q < Profiles.Length; q++)
			{
					GUILayout.Space((Sw*Sh)/39322);
					if(GUILayout.Button(Profiles[q], GUILayout.Width(Sw/8), GUILayout.Height(Sh/15))&&Profiles[q]!="")
					{
						if(!once)
						{
							profile = Profiles[q];
							Instantiate(storage,transform.position,transform.rotation);
							storageObject = GameObject.FindWithTag("storage");
							storageObject.GetComponent<storage>().currentProfile = profile;
							Instantiate(soundClick,transform.position,transform.rotation);
							Application.LoadLevel(levelLoad);
							once = true;
						}
					}
			}
			GUILayout.EndHorizontal();

			GUILayout.EndVertical();
			GUILayout.EndArea();
			if (GUI.Button(new Rect(Sw*.7f-50, Sh*.85f-20, 100, 40), "DeleteAll"))
			{
				PlayerPrefs.DeleteAll();
				Instantiate(soundClick,transform.position,transform.rotation);
				Application.LoadLevel("mainMenu");
			}
			if (GUI.Button(new Rect(Sw*.3f-50, Sh*.85f-20, 100, 40), "Back"))
			{
				newGameText = 0;
				Instantiate(soundClick,transform.position,transform.rotation);
			}
		}
	}
}
